﻿Imports ISNet.WebUI.WebGrid
Imports System
Imports System.Data
Imports System.Configuration
Imports System.Collections
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Web.UI.HtmlControls
Imports log4net

Partial Class mtAppConfigs
    Inherits System.Web.UI.Page

    Private Shared logger As ILog = LogManager.GetLogger("mtAppConfigs")

    Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load

        ScreenName.Text = "ระบบจัดทำงบประมาณ  >>  ตั้งค่าระบบ >>  App Config"
        'Session("users") = "bb0011"
        If Session("users") Is Nothing Then
            Response.Redirect("LoginForm.aspx")
        End If
        
        If IsPostBack = False Then
            PlanYearsTxt.Text = Session("PlanYear")
            Session("PlanYearTxt") = PlanYearsTxt.Text
            Session("SearchTxt") = ""
            Session("SearchTxt") = " and PlanYear = " & PlanYearsTxt.Text
        Else
            'PlanYearsTxt.Text = Session("PlanYearTxt")
        End If

    End Sub

    Protected Sub WebGrid1_InitializeDataSource(ByVal sender As Object, ByVal e As ISNet.WebUI.WebGrid.DataSourceEventArgs) Handles WebGrid1.InitializeDataSource
        Dim adapter As New MasterDataSetTableAdapters.AppConfigsTableAdapter
        Dim dtTable As New MasterDataSet.AppConfigsDataTable

        Dim sql As String = "select * from AppConfigs "
        sql += " WHERE 1 = 1 "

        'If PlanYearsTxt.Text <> "" Then
        '    sql += " and PlanYear like '" & PlanYearsTxt.Text & "%'"
        'End If

        If SearchTxt.Text = "" Then
            Session("SearchTxt") = " and Name like '%%'"
        Else
            sql += Session("SearchTxt")
        End If

        sql += " ORDER BY Name "

        adapter.SearchAppConfig(dtTable, sql)
        'Response.Write(sql)

        Session("count_data") = dtTable.Rows.Count

        dtTable.Columns("Value").AllowDBNull = True
        dtTable.Columns("Name").AllowDBNull = True
        dtTable.Columns("Description").AllowDBNull = True

        e.DataSource = dtTable

    End Sub

    Protected Sub WebGrid1_InitializeRow(ByVal sender As Object, ByVal e As ISNet.WebUI.WebGrid.RowEventArgs)
        ' don't edit column
        'e.Row.Cells(1).ForceNoEdit = True
        e.Row.Cells(4).ForceNoEdit = True
    End Sub

    Protected Sub WebGrid1_AddRow(ByVal sender As Object, ByVal e As ISNet.WebUI.WebGrid.RowEventArgs)
        Dim adapter As New MasterDataSetTableAdapters.AppConfigsTableAdapter

        Dim check = adapter.CheckExistAppConfig(e.Row.Cells(1).Value, e.Row.Cells(2).Value, Trim(e.Row.Cells(3).Value))

        If Val(check) <> 0 Then
            WebGrid1.ClearCachedDataSource()
            WebGrid1.ClientAction.Refresh()
            Throw New ArgumentException("ข้อมูล Budget Version ซ้ำกับที่มีอยู่ กรุณาตรวจสอบ")
        Else
            Dim id As Decimal
            id = adapter.InsertAppConfig(e.Row.Cells(1).Value, e.Row.Cells(2).Value, e.Row.Cells(3).Value, Date.Now, Session("users"))
            Dim dr As DataRow = e.Row.DataRow
            dr("id") = id

            logger.Info("User " & Session("users") & " insert app config " & e.Row.Cells(1).Value)
        End If
    End Sub

    Protected Sub WebGrid1_UpdateRow(ByVal sender As Object, ByVal e As ISNet.WebUI.WebGrid.RowEventArgs)
        Dim id As String
        id = e.Row.KeyValue
        'WebGrid1.ClientAction.Alert(iyear)

        Dim adapter As New MasterDataSetTableAdapters.AppConfigsTableAdapter
        Dim check = adapter.CheckExistAppConfig(e.Row.Cells(1).Value, e.Row.Cells(2).Value, Trim(e.Row.Cells(3).Value))

        If Val(check) > 1 Then
            WebGrid1.ClearCachedDataSource()
            WebGrid1.ClientAction.Refresh()
            Throw New ArgumentException("ข้อมูล Budget Version ซ้ำกับที่มีอยู่ กรุณาตรวจสอบ")
        Else
            If adapter.FillByYear(id, Session("PlanYear")) > 0 Then
                Session("PlanYear") = e.Row.Cells(2).Value
            End If
            adapter.UpdateAppConfig(e.Row.Cells(1).Value, e.Row.Cells(2).Value, e.Row.Cells(3).Value, Date.Now, Session("users"), id)

            logger.Info("User " & Session("users") & " update app config " & e.Row.Cells(1).Value)
        End If

    End Sub

    Protected Sub WebGrid1_InitializePostBack(ByVal sender As Object, ByVal e As ISNet.WebUI.WebGrid.PostbackEventArgs) Handles WebGrid1.InitializePostBack
        If e.Action = PostBackAction.Custom Then
            Dim type As String = TryCast(Request("type"), String)
            Dim Deletet As String = TryCast(Request("id"), String)

            Dim adapter As New MasterDataSetTableAdapters.AppConfigsTableAdapter

            ' MsgBox(idd & "/" & idy)
            If type = "1" Then

                adapter.DeleteAppConfig(Deletet)
                logger.Info("User " & Session("users") & " delete app config " & Deletet)

            ElseIf type = "2" Then

                For Each keyValue As String In WebGrid1.RootTable.GetCheckedRows()
                    adapter.DeleteAppConfig(keyValue)
                Next keyValue

            End If

            WebGrid1.ClientAction.Refresh()
        End If

    End Sub
    Protected Sub SearhBtn_Click(sender As Object, e As ImageClickEventArgs) Handles SearhBtn.Click
        Session("SearchTxt") = ""
        If SearchTxt.Text <> "" Then
            Session("SearchTxt") += " and Name like '%" & SearchTxt.Text & "%'"
        End If
        WebGrid1.ClearCachedDataSource()
        WebGrid1.RebindDataSource()
        t_countdata.Visible = True
        If Val(Session("count_data")) > 0 Then
            L_CountData.Text = FormatNumber(Val(Session("count_data")), 0) & " รายการ"
        Else
            L_CountData.Text = "ไม่พบข้อมูลที่ค้นหา"
        End If
    End Sub

    Protected Sub SaveBtn_Click(sender As Object, e As ImageClickEventArgs) Handles SaveBtn.Click
        WebGrid1.ClearCachedDataSource()
        WebGrid1.RebindDataSource()
    End Sub

End Class
